<?php
    include 'connectdb.php';
    $arr = array();
    $i = 0;
    $sql = "SELECT * FROM home_stay_room where home_stay_id =".$_POST['home_stay_name'];
    $query = mysqli_query($link, $sql);
    while ($row = mysqli_fetch_array($query)) {
        $home_stay_room_id = $row['home_stay_room_id'];
        $sql = "SELECT * FROM `booking_detail` AS bd 
                WHERE bd.`home_stay_room_id`= $home_stay_room_id 
                AND (bd.`date` BETWEEN '".$_POST['date1']."' AND '".$_POST['date2']."') 
                AND bd.status_id <> 5";
        $query2 = mysqli_query($link, $sql);
        $rows = mysqli_fetch_array($query2);
    if( isset($rows['home_stay_room_id']) ){
        $arr[$i] = $home_stay_room_id;
        $i++;
    }
}
$in = '';
foreach($arr as $k => $v){
    $in .= $v.',';
}
if($in != '')
    $in = substr($in, 0, $length-1);
else
    $in = 0;
    ($in);
    $q2 = "select * from home_stay_room where home_stay_id = '".$_POST['home_stay_name']."' AND home_stay_room_id NOT IN ($in)";
    $res = mysqli_query($link, $q2);
?>
<table border="1" width="764">
    <tr>
        <?php
        $q3 = "select * from home_stay where home_stay_id = '".$_POST['home_stay_name']."'";
        $res3 = mysqli_query($link,$q3);
        $row3 = mysqli_fetch_array($res3);
        ?>        
        <td colspan='5'><strong><font size="4"><?php echo $row3['home_stay_name'];?></font></strong></td>
    </tr>
    <tr>
        <td width="100" align="center">ชื่อห้อง</td>
        <td align="center">รายละเอียด</td>
        <td width="100" align="center">ราคา</td>
        <td width="100" align="center">จอง</td>
    </tr>
    <?php
        while( $row2 = mysqli_fetch_array($res)){
    ?>
    <tr height="120">
        <td>
            <?php echo $row2['home_stay_room_name'];?></br>
            <?php 
                if ($row['home_stay_room_picture']==''){
            ?>
                <img src="images/no_image/no_image.jpeg" width="72" height="72"></td>
            <?php
                }
                else {
                    echo '<img src='.$row['home_stay_room_picture'].'></td>';
                }
            ?>
        <td valign="top"><br/><?php echo nl2br($row2['home_stay_room_detail']);?></td>
        <td align="center">
            (<?php echo $_POST['hiddenDate'];?>&nbsp;วัน)<br/><?php $total = $row2['price_per_night'] * $_POST['hiddenDate']; $deposit = ($row2['price_per_night'] * $_POST['hiddenDate']) / 2; echo $total;?>&nbsp;บาท
        </td>
        <td align="center"><a href="index.php?save=true&r_id=<?php echo $row2['home_stay_room_id'];?>&date1=<?php echo $_POST['date1'];?>&date2=<?php echo $_POST['date2'];?>&night=<?php echo $_POST['hiddenDate'];?>&price=<?php echo $total;?>&deposit=<?php echo $deposit;?>"/>จอง</a></td>
    </tr>
    <?php
        }
     ?>   
</table>